package com.haoshuang.zhouzhoubang.ui.page.message.child.error

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ItemErrorMessageBinding
import com.haoshuang.zhouzhoubang.model.bean.ErrorMessageBean
import com.haoshuang.zhouzhoubang.ui.dialog.HelpDescriptionDialog
import com.haoshuang.zhouzhoubang.utils.DateTools
import com.haoshuang.zhouzhoubang.widget.UploadFileBean
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.utils.ktx.convertDouble
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone


/**
 * 
 */
class ErrorMessageAdapter(data: MutableList<ErrorMessageBean>) : BaseBindingQuickAdapter<ErrorMessageBean, ItemErrorMessageBinding>(data) {
    private var countDownTimer: CountDownTimer? = null
    override fun onViewRecycled(holder: RecyclerView.ViewHolder) {
        super.onViewRecycled(holder)
        countDownTimer?.cancel()
        countDownTimer = null
    }

    override fun onBindViewHolder(holder: BaseBindingHolder, position: Int, item: ErrorMessageBean?) {
        holder.getViewBinding<ItemErrorMessageBinding>().apply {

            when (item?.feedback_type) {
                1 -> tvTitle.text = "订单投诉"
                2 -> tvTitle.text = "意见反馈"
                3 -> tvTitle.text = "返工售后"
                4 -> tvTitle.text = "违规处理"
                5 -> tvTitle.text = "差评消息"
            }
            // 右上角的状态
            tvStatus.text = item?.status_text
            if (item?.feedback_type == 3){
                if (!item?.compensation_mind_amount.isNullOrEmpty() && item?.compensation_mind_amount.convertDouble() > 0.0){
                    // 显示补偿的诚意金
                    llCompensationAmount.visible()
                    tvCompensationAmount.text = "${item.compensation_mind_amount}元"
                }else{
                    llCompensationAmount.gone()
                }
            }

            // 服务名称
            tvTitleName.text = item?.store_name
            // 用户姓名
            tvName.text = item?.real_name
            // 用户手机号
            tvPhone.text = item?.user_phone?.replaceRange(3, 7, "****")
            // 用户地址
            tvAddress.text = item?.user_address
            // 投诉说明
            tvContent.text = item?.content
            // 证据图片
            val list = mutableListOf<String>()
            if (item?.feedback_type == 3) {
                list.addAll(item?.apply_reason_images?.toMutableList() ?: mutableListOf())
            }else{
                list.addAll(item?.pics?.toMutableList() ?: mutableListOf())
            }

            cuvImages.setMediaList(list.filter { it.isNotEmpty() }.map { UploadFileBean(null,it,0, false, 2) })
            // 违规扣除金额
            llIllegalDeductions.visibleOrGone(!item?.liquidated_damages.isNullOrEmpty() && item?.liquidated_damages.convertDouble() > 0.0)
            tvIllegalDeductions.clickWithTrigger {
                // 显示违规扣除弹窗
                XPopup.Builder(context)
                    .popupPosition(PopupPosition.Bottom)
                    .animationDuration(0)
                    .borderRadius(SizeUtils.dp2px(10f).toFloat())
                    .hasShadowBg(false)
                    .atView(tvIllegalDeductions)
                    .asCustom(HelpDescriptionDialog(context, "申诉失败时违规扣除金额，将从提现金额中扣除。"))
                    .show()
            }
            tvMoney.text = "￥${item?.liquidated_damages}"

            // 违规扣除积分
            llIllegalDeductionsIntegral.visibleOrGone(!item?.integral.isNullOrEmpty() && item?.integral.convertDouble() > 0.0)
            tvMoneyIntegral.text = item?.integral

            // 拒绝原因
            llRejectReason.visibleOrGone(!item?.remark.isNullOrEmpty())
            tvRejectReason.text = item?.remark

            // 不可申诉原因显示
            tvDeregulation.visibleOrGone(item?.is_master_agree == 2 && item.feedback_type in arrayListOf(1,2,4,5))
            if (item?.is_master_agree == 2){
                tvStatus.text = "不可申诉"
            }

            // 申诉倒计时
            tvTimeOut.visibleOrGone(item?.is_appeal == 1 && item.feedback_type in arrayListOf(1,2,4,5) && item.appeal_status == "-1" && item.time_countdown != 0)

            val time = item?.time_countdown?:0
            if (time == 0){
                tvTimeOut.text = "已超时"
            }else{
                countDownTimer =
                    object : CountDownTimer(time * 1000L, 1 * 1000) {
                        @SuppressLint("SimpleDateFormat")
                        override fun onTick(millisUntilFinished: Long) {
                            val s: String =
                                DateTools.millisecondsConvertToHMS(millisUntilFinished)
                            tvTimeOut.text = "剩$s"
                        }

                        override fun onFinish() {
                            tvTimeOut.text = "已超时"
                            tvTimeOut.gone()
                            notifyItemChanged(holder.layoutPosition)
                        }
                    }
                countDownTimer?.start()
            }

            when(item?.feedback_type){
                1 -> {
                    tvAgree.gone()
                    tvRefuse.gone()
                    when(item.status){
                        "-1" -> {
                            // 可以申诉
                            tvAgreePunishment.visible()
                            tvFeedback.visible()
                        }
                        else -> {
                            tvAgreePunishment.gone()
                            tvFeedback.gone()
                        }
                    }
                }
                2 -> {

                }
                3 -> {
                    tvAgreePunishment.gone()
                    tvFeedback.gone()
                    // 返工消息
                    when (item.status) {
                        "0" -> {
                            tvAgree.visible()
                            tvRefuse.visible()
                        }

                        "1","2","3","4" -> {
                            tvAgree.gone()
                            tvRefuse.gone()
                        }
                    }
                }
                4 -> {

                }
                5 -> {
                    tvAgree.gone()
                    tvRefuse.gone()
                    // 其他消息
//                tvAgreePunishment.visibleOrGone(item.feedback_type != 3)
                    when(item?.appeal_status){
                        "-1" -> {
                            // 可以申诉
                            tvAgreePunishment.visibleOrGone(item.feedback_type != 5)
                            tvFeedback.visible()
                        }
                        "0" -> {
                            // 申诉待审核
                            if (item.feedback_type == 3){
                                // 显示申诉详情
                                tvFeedbackDetail.visible()
                            }
                            tvAgreePunishment.gone()
                            tvFeedback.gone()
                        }
                        "1" -> {
                            // 申诉通过
                            tvAgreePunishment.gone()
                            tvFeedback.gone()
                            if (item.feedback_type == 3){
                                // 显示申诉详情
                                tvFeedbackDetail.visible()
                            }
                        }
                        "2" -> {
                            // 拒绝申诉
                            tvAgreePunishment.gone()
                            tvFeedback.gone()
                            if (item.feedback_type == 3){
                                // 显示申诉详情
                                tvFeedbackDetail.visible()
                            }
                        }
                        "3" -> {
                            // 已过申诉期
                            tvAgreePunishment.gone()
                            tvFeedback.gone()
                        }
                    }
                }
            }

            if (item?.is_deal == 1){
                llDeal.gone()
            }else{
                llDeal.visible()
            }
        }
    }

}